from spider_utils import *
from weibo import *
import re

main_url = 'https://m.weibo.cn/api/container/getIndex?containerid='


def extract_weibo_by_uid(uid):
    """
    提取微博信息（除内容详情detail）
    :param uid: weibo中的uid
    :return:
    """
    page_num = 1000
    container_id = trans_uid_to_containerid(uid)
    url = main_url + container_id + '_-_WEIBO_SECOND_PROFILE_WEIBO&page_type=03&page='

    for i in range(page_num):
        page_url = url + str(i)
        j_content = requests.get(page_url).json()
        print('第 ' + str(i) + ' 页: ' + page_url)
        for j in range(len(j_content['data']['cards'])):
            if 'mblog' in j_content['data']['cards'][j].keys():
                wb = WeiboRecord()
                wb.url = j_content['data']['cards'][j]['scheme']
                print(wb.url)
                wb.weibo_uid = uid
                wb.comment_num = j_content['data']['cards'][j]['mblog']['comments_count']
                wb.thumps_num = j_content['data']['cards'][j]['mblog']['attitudes_count']
                wb.repost_num = j_content['data']['cards'][j]['mblog']['reposts_count']
                wb.mid = j_content['data']['cards'][j]['mblog']['mid']

                wb.create_time = trans_format(j_content['data']['cards'][j]['mblog']['created_at'])
                # 添加微博数据
                orm.add(wb)


def extract_detail():
    """
    提取每条微博的详情内容并入库
    :param url:  该条微博的详情页链接
    :return:
    """

    wb_list = orm.query_all(WeiboRecord)
    for wb in wb_list:
        wb_url = wb.url
        param_value = re.search('status/(.*)\?', wb_url).group(1)
        url = 'https://m.weibo.cn/statuses/show?id=' + param_value

        j_content = requests.get(url).json()
        wb.detail = re.sub('<(\S*?)[^>]*>.*?|<.*? />', '', j_content['data']['text'])
        orm.update(WeiboRecord, {'id': wb.id}, {'detail': wb.detail})


def extract_comments(id, max_id_type=0):
    pre_url = 'https://m.weibo.cn/comments/hotflow?'
    first_page = pre_url + 'id=' + id + '&mid=' + id + '&max_id_type=' + max_id_type



if __name__ == '__main__':
    # 提取所有微博
    # extract_weibo_by_uid(5576168164)
    # 提取详情
    # extract_detail()
    # 提取评论

    print(requests.get('https://m.weibo.cn/statuses/show?id=Koyv9snVo').json())
